package edu.zju.gis.dbfg.server.constant;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserRoleTypeHandler implements TypeHandler<UserRole> {

  /**
   * 定义当前数据如何保存到数据库中
   * @param preparedStatement
   * @param i
   * @param userRole
   * @param jdbcType
   * @throws SQLException
   */
  @Override
  public void setParameter(PreparedStatement preparedStatement, int i, UserRole userRole, JdbcType jdbcType) throws SQLException {
    preparedStatement.setString(i, userRole.getName());
  }

  /**
   * 根据数据库中的值拿到返回的枚举类
   * @param resultSet
   * @param s
   * @return
   * @throws SQLException
   */
  @Override
  public UserRole getResult(ResultSet resultSet, String s) throws SQLException {
    String value = resultSet.getString(s);
    return UserRole.find(value);
  }

  @Override
  public UserRole getResult(ResultSet resultSet, int i) throws SQLException {
    String value = resultSet.getString(i);
    return UserRole.find(value);
  }

  @Override
  public UserRole getResult(CallableStatement callableStatement, int i) throws SQLException {
    String value = callableStatement.getString(i);
    return UserRole.find(value);
  }
}
